home *** CD-ROM | disk | FTP | other *** search
/ Internet.Works 40 / Issue 40.iso / pc / PCSoftware / DHTML Menu Builder 2.6 / dhtmlmen.exe / %AppDir% / AddIns / HideOnClick.ext < prev   
Encoding:
Text File  |  2000-10-26  |  16.0 KB  |  564 lines

  1. This AddIn will change the default behavior of the menus, so they hide only when the document is clicked.
  2.  
  3. NOTE: This AddIn may not work with frames support turned on.
  4.  
  5. Developed by Xavier Flix
  6.  
  7. ***
  8. //COPYRIGHT
  9.     
  10.     var nStyle = new Array;
  11.     var hStyle = new Array;
  12.     var nLayer = new Array;
  13.     var hLayer = new Array;
  14.     var nTCode = new Array;
  15.     
  16.     var AnimStep = 0;
  17.     var AnimHnd = 0;
  18.     var HTHnd = new Array;
  19.     var DoFormsTweak = false;
  20.     
  21.     var mFrame;
  22.     var cFrame;
  23.     
  24.     var OpenMenus = new Array;
  25.     var SelCommand;
  26.     var nOM = 0;
  27.     
  28.     var mX;
  29.     var mY;
  30.     var xOff = 0;
  31.     
  32.     var HideSpeed = 200;
  33.     
  34. //BROWSERDETECTION
  35.     
  36.     if ((frames.length==0) && IsMac)
  37.     frames.top = window;
  38.     
  39.     if(IE)
  40.     xOff = 2;
  41. //CODEFRAME
  42.     
  43. //FX
  44.     
  45. //STYLECODE
  46.  
  47.     function GetCurCmd() {
  48.         //IE
  49.         //This function will return the current command under the mouse pointer. It will return null if the mouse is not over any command.
  50.         //------------------------------
  51.         //Version 1.3
  52.         //
  53.         var cc = mFrame.window.event.srcElement;
  54.         while((cc.id=="") && (cc.tagName!="TD")) {
  55.             cc = cc.parentElement;
  56.             if(cc==null)
  57.                 break;
  58.         }
  59.         return cc;
  60.     }
  61.  
  62.     function HoverSel(mode, imgLName, imgRName) {
  63.         //IE
  64.         //This is the function called every time the mouse pointer is moved over or away from a command.
  65.         //------------------------------
  66.         //mode: 0 if the mouse is moving over the command and 1 if is moving away
  67.         //imgName: Name of the image object, if any, used in the selected command
  68.         //------------------------------
  69.         //Version 8.0
  70.         //
  71.         var imgL = new Image;
  72.         var imgLRsc = new Image;
  73.         var imgR = new Image;
  74.         var imgRRsc = new Image;
  75.         var mc;
  76.         
  77.         if(mode==0 && OpenMenus[nOM].SelCommand!=null)
  78.             HoverSel(1);
  79.         
  80.         if(imgLName!="_")
  81.             var imgL = eval("mFrame.document.images['"+imgLName+"']");
  82.         if(imgRName!="_")
  83.             var imgR = eval("mFrame.document.images['"+imgRName+"']");
  84.         
  85.         if(mode==0) {
  86.             mc = GetCurCmd();
  87.             if(nOM>1)
  88.                 if(mc==OpenMenus[nOM-1].SelCommand)
  89.                     return false;
  90.             if(OpenMenus[nOM].SelCommand || nOM>1)
  91.                 while(!InMenu())
  92.                     Hide();
  93.             mc.style.cssText = hStyle[mc.id];
  94.             if(imgLName!='_') imgLRsc = eval(imgLName+"On");
  95.             if(imgRName!='_') imgRRsc = eval(imgRName+"On");
  96.             OpenMenus[nOM].SelCommand = mc;
  97.             OpenMenus[nOM].SelCommandPar = [imgLName,imgRName];
  98.         } else {
  99.             mc = (mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
  100.             imgLName = (mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
  101.             imgRName = (mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
  102.             mc.style.background = "";
  103.             mc.style.cssText = nStyle[mc.id];
  104.             if(imgLName!='_') imgLRsc = eval(imgLName+"Off");
  105.             if(imgRName!='_') imgRRsc = eval(imgRName+"Off");
  106.             window.status = "";
  107.             OpenMenus[nOM].SelCommand = null;            
  108.         }
  109.         
  110.         if(imgLName!='_') {
  111.             imgL = eval("mFrame.document.images."+imgLName);
  112.             imgL.src = imgLRsc.src;
  113.         }
  114.         if(imgRName!='_') {
  115.             imgR = eval("mFrame.document.images."+imgRName);
  116.             imgR.src = imgRRsc.src;
  117.         }
  118.         
  119.         return true;
  120.     }
  121.  
  122.     function NSHoverSel(mode, mc, bcolor, w, h) {
  123.         //NS
  124.         //This is the function called every time the mouse pointer is moved over or away from a command.
  125.         //------------------------------
  126.         //mc: Name of the layer that corresponds to the selected command
  127.         //mode: 0 if the mouse is moving over the command and 1 if is moving away
  128.         //n: Unique ID that identifies this command. Used to retrieve the data from the nLayer or hLayer array.
  129.         //bcolor: Background color of the command. Ignored if the group uses a background image.
  130.         //w: Width of the command's layer.
  131.         //h: Height of the command's layer.
  132.         //------------------------------
  133.         //Version 10.0
  134.         //
  135.         var n;
  136.         var LayerHTM;
  137.         if(mode==0 && OpenMenus[nOM].SelCommand!=null)
  138.             NSHoverSel(1);
  139.         
  140.         if(mode==0) {
  141.             n = CBparseInt(mc.name.substr(2));
  142.             if(nOM>1)
  143.                 if(mc==OpenMenus[nOM-1].SelCommand)
  144.                     return false;
  145.             while(!InMenu())
  146.                 Hide();
  147.             LayerHTM = hLayer[n];
  148.             OpenMenus[nOM].SelCommand = mc;
  149.             OpenMenus[nOM].SelCommandPar = [mc.bgColor,w,h];
  150.             mc.bgColor = bcolor;
  151.         } else {
  152.             mc = (mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
  153.             bcolor = (mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
  154.             w = (mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
  155.             h = (mode==1)?OpenMenus[nOM].SelCommandPar[2]:OpenMenus[nOM].OpenerPar[2];
  156.             n = CBparseInt(mc.name.substr(2));
  157.             LayerHTM = nLayer[n];
  158.             if(mc.parentLayer.background.src!="")
  159.                 mc.bgColor = null;
  160.             else
  161.                 mc.bgColor = bcolor;
  162.             window.status = "";
  163.             OpenMenus[nOM].SelCommand = null;
  164.         }
  165.         mc.resizeTo(w,h);
  166.         mc.document.open();
  167.         mc.document.write(LayerHTM);
  168.         mc.document.close();
  169.         
  170.         return true;
  171.     }
  172.  
  173.     function Hide() {
  174.         //IE,NS
  175.         //This function hides the group identified by the menuId parameter.
  176.         //It also resets the values of the CurMenu and ParentMenu variables to reflect the changes of the hidden menu.
  177.         //------------------------------
  178.         //menuId: Name of the group's <div> or <layer> to be hidden.
  179.         //------------------------------
  180.         //Version 2.1
  181.         //
  182.         
  183.         if(AnimHnd)
  184.             window.clearTimeout(AnimHnd);
  185.         
  186.         if(OpenMenus[nOM].SelCommand!=null) {
  187.             if(IE) HoverSel(1);
  188.             if(NS) NSHoverSel(1);
  189.         }
  190.         if(OpenMenus[nOM].Opener!=null) {
  191.             if(IE) HoverSel(3);
  192.             if(NS) NSHoverSel(3);
  193.         }
  194.         
  195.         OpenMenus[nOM].visibility = "hidden";
  196.         window.clearTimeout(HTHnd[nOM]);
  197.         HTHnd[nOM] = 0;
  198.         nOM--;
  199.         
  200.         if(nOM>0)
  201.             if(!InMenu())
  202.                 HTHnd[nOM] = window.setTimeout("Hide()", HideSpeed);
  203.         
  204.         if(nOM==0)
  205.             FormsTweak("visible");
  206.     }
  207.  
  208.     function ShowMenu(mName, x, y, isCascading) {
  209.         //IE,NS
  210.         //This is the main function to show the menus when a hotspot is triggered or a cascading command is activated.
  211.         //------------------------------
  212.         //menuId: Name of the <div> or <layer> to be shown.
  213.         //x: Left position of the menu.
  214.         //y: Top position of the menu.
  215.         //isCascading: True if the menu has been triggered from a command, and not from a hotspot.
  216.         //------------------------------
  217.         //Version 12.9
  218.         //        
  219.         //(as) {
  220.         //indow.clearTimeout(as);
  221.         //s = 0;
  222.         //
  223.         x = CBparseInt(x);
  224.         y = CBparseInt(y);
  225.         if(AnimHnd && nOM>0) {
  226.             AnimStep=100;
  227.             Animate();
  228.         }
  229.         if(IE)
  230.             var Menu = mFrame.document.all[mName];
  231.         if(NS)
  232.             var Menu = mFrame.document.layers[mName];
  233.         if(!Menu)
  234.             return false;
  235.         if(IE)
  236.             Menu = Menu.style;
  237.         if(Menu==OpenMenus[nOM] || HTHnd[nOM])
  238.             return false;
  239.         
  240.         Menu.Opener = nOM>0?OpenMenus[nOM].SelCommand:null;
  241.         Menu.OpenerPar = nOM>0?OpenMenus[nOM].SelCommandPar:null;
  242.         Menu.SelCommand = null;
  243.         
  244.         if(!isCascading)
  245.             HideAll();
  246.         
  247.         var pW = GetWidthHeight()[0] + GetLeftTop()[0];
  248.         var pH = GetWidthHeight()[1] + GetLeftTop()[1];
  249.         
  250.         if(IE) {
  251.             if(isCascading) {
  252.                 x = CBparseInt(OpenMenus[nOM].left) + CBparseInt(OpenMenus[nOM].width) - 6;
  253.                 y = y + CBparseInt(OpenMenus[nOM].top) - 5;
  254.                 Menu.left = (x+CBparseInt(Menu.width)>pW)?CBparseInt(OpenMenus[nOM].left) - CBparseInt(Menu.width) + 6:x;
  255.                 Menu.top =  (y+CBparseInt(Menu.height)>pH)?pH - CBparseInt(Menu.height):y;
  256.             } else {
  257.                 Menu.left = (x+CBparseInt(Menu.width)>pW)?pW - CBparseInt(Menu.width):x;
  258.                 Menu.top =  (y+CBparseInt(Menu.height)>pH)?pH - CBparseInt(Menu.height):y;
  259.             }
  260.             if(!IsMac)
  261.                 Menu.clip = "rect(0 0 0 0)";
  262.         }
  263.         if(NS) {
  264.             if(isCascading) {
  265.                 x = OpenMenus[nOM].left + OpenMenus[nOM].clip.width - 6;
  266.                 y = OpenMenus[nOM].top + OpenMenus[nOM].SelCommand.top;
  267.                 x = (x+Menu.w>pW)?OpenMenus[nOM].left - Menu.w + 6:x;
  268.                 y = (y+Menu.h>pH)?pH - Menu.h:y;
  269.             } else {
  270.                 x = (x+Menu.w>pW)?pW - Menu.w:x;
  271.                 y = (y+Menu.h>pH)?pH - Menu.h:y;
  272.             }
  273.             Menu.clip.width = 0;
  274.             Menu.clip.height = 0;
  275.             Menu.moveToAbsolute(x,y);
  276.         }
  277.         if(isCascading)
  278.             Menu.zIndex = CBparseInt(OpenMenus[nOM].zIndex) + 1;
  279.         Menu.visibility = "visible";
  280.         OpenMenus[++nOM] = Menu;
  281.         HTHnd[nOM] = 0;
  282.         if((IE && !IsMac) || NS)
  283.             AnimHnd = window.setTimeout("Animate()", 10);
  284.         FormsTweak("hidden");
  285.         
  286.         return true;
  287.     }
  288.  
  289.     function Animate() {
  290.         //IE,NS
  291.         //This function is called by ShowMenu every time a new group must be displayed and produces the predefined unfolding effect.
  292.         //Currently is disabled for Navigator, because of some weird bugs we found with the clip property of the layers.
  293.         //------------------------------
  294.         //menuId: Name of the <div> or <layer> to be animated.
  295.         //------------------------------
  296.         //Version 1.3
  297.         //
  298.         var r = '';
  299.         var nw = nh = 0;
  300.         switch(fx) {
  301.             case 1:
  302.                 if(IE) r = "0 " + AnimStep + "% " + AnimStep + "% 0";
  303.                 if(NS) nw = AnimStep; nh = AnimStep;
  304.                 break;
  305.             case 2:
  306.                 if(IE) r = "0 100% " + AnimStep + "% 0";
  307.                 if(NS) nw = 100; nh = AnimStep;
  308.                 break;
  309.             case 3:
  310.                 if(IE) r = "0 " + AnimStep + "% 100% 0";
  311.                 if(NS) nw = AnimStep; nh = 100;
  312.                 break;
  313.             case 0:
  314.                 if(IE) r = "0 100% 100% 0";
  315.                 if(NS) nw = 100; nh = 100;
  316.                 break;
  317.         }
  318.         with(OpenMenus[nOM]) {
  319.             if(IE)
  320.                 clip =  "rect(" + r + ")";
  321.             if(NS) {
  322.                 clip.width = w*(nw/100);
  323.                 clip.height = h*(nh/100);
  324.             }
  325.         }
  326.         AnimStep += 20;
  327.         if(AnimStep<=100)
  328.             AnimHnd = window.setTimeout("Animate()",25);
  329.         else {
  330.             window.clearTimeout(AnimHnd);
  331.             AnimStep = 0;
  332.             AnimHnd = 0;
  333.         }
  334.     }
  335.  
  336.     function InMenu() {
  337.         //IE,NS
  338.         //This function returns true if the mouse pointer is over a group.
  339.         //------------------------------
  340.         //mX: Current X position of the mouse pointer.
  341.         //mY: Current Y position of the mouse pointer.
  342.         //m: <div> or <layer> object to be tested.
  343.         //------------------------------
  344.         //Version 1.2
  345.         //
  346.         var m = OpenMenus[nOM];
  347.         if(!m)
  348.             return false;
  349.         if(IE&&BV==4)
  350.             SetPointerPos();
  351.         var l = CBparseInt(m.left) + xOff;
  352.         var r = l+((IE)?CBparseInt(m.width):m.clip.width) - xOff;
  353.         var t = CBparseInt(m.top) + xOff;
  354.         var b = t+((IE)?CBparseInt(m.height):m.clip.height) - xOff;
  355.         return ((mX>=l && mX<=r) && (mY>=t && mY<=b));
  356.     }
  357.  
  358.     function SetPointerPos(e) {
  359.         //IE,NS
  360.         //This function sets the mX and mY variables with the current position of the mouse pointer.
  361.         //------------------------------
  362.         //e: Only used under Navigator, corresponds to the Event object.
  363.         //------------------------------
  364.         //Version 1.0
  365.         //
  366.         if(IE) {
  367.             if(event==null)
  368.                 if(mFrame.window.event==null)
  369.                     return;
  370.                 else
  371.                     e = mFrame.window.event;
  372.             else
  373.                 e = event;
  374.             mX = e.clientX + mFrame.document.body.scrollLeft;
  375.             mY = e.clientY + mFrame.document.body.scrollTop;
  376.         }
  377.         if(NS) {
  378.             mX = e.pageX;
  379.             mY = e.pageY;
  380.         }
  381.     }
  382.  
  383.     function HideMenus(e) {
  384.         //IE,NS
  385.         //This function checks if the mouse pointer is on a valid position and if the current menu should be kept visible.
  386.         //The function is called every time the mouse pointer is moved over the document area.
  387.         //------------------------------
  388.         //e: Only used under Navigator, corresponds to the Event object.
  389.         //------------------------------
  390.         //Version 24.0
  391.         //
  392.         SetPointerPos(e);
  393.     }
  394.  
  395.     function FormsTweak(state) {
  396.         //IE
  397.         //This is an undocumented function, which can be used to hide every form element on a page.
  398.         //This can be useful if the menus will be displayed over an area where is a combo box, which is an element that cannot be placed behind the menus and it will always appear over the menus resulting in a very undesirable effect.
  399.         //------------------------------
  400.         //Version 1.0
  401.         //
  402.         if(DoFormsTweak && IE)
  403.             for(var f = 0; f <= (mFrame.document.forms.length - 1); f++)
  404.                 for(var e = 0; e <= (mFrame.document.forms[f].elements.length - 1); e++)
  405.                     if(mFrame.document.forms[f].elements[e].type=="select-one")
  406.                         mFrame.document.forms[f].elements[e].style.visibility = state;
  407.     }
  408.  
  409.     function execURL(url, tframe) {
  410.         //IE,NS
  411.         //This function is called every time a command is triggered to jump to another page or execute some javascript code.
  412.         //------------------------------
  413.         //url: Encrypted URL that must be opened or executed.
  414.         //tframe: If the url is a document location, tframe is the target frame where this document will be opened.
  415.         //------------------------------
  416.         //Version 1.0
  417.         //
  418.         HideAll();
  419.         window.setTimeout("execURL2('" + url + "', '" + tframe + "')", 100);
  420.     }
  421.  
  422.     function execURL2(url, tframe) {
  423.         //IE,NS
  424.         //This function is called every time a command is triggered to jump to another page or execute some javascript code.
  425.         //------------------------------
  426.         //url: Encrypted URL that must be opened or executed.
  427.         //tframe: If the url is a document location, tframe is the target frame where this document will be opened.
  428.         //------------------------------
  429.         //Version 1.0
  430.         //
  431.         tframe = rStr(tframe);
  432.         var fObj = eval(tframe);
  433.         url = rStr(url);
  434.         if(url.indexOf("javascript")!=url.indexOf("vbscript"))
  435.             eval(url);
  436.         else
  437.             fObj.location.href = url;
  438.     }
  439.  
  440.     function rStr(s) {
  441.         //IE,NS
  442.         //This function is used to decrypt the URL parameter from the triggered command.
  443.         //------------------------------
  444.         //Version 1.1
  445.         //
  446.         s = xrep(s,"\x1E","'");
  447.         s = xrep(s,"\x1D","\x22");
  448.         s = xrep(s,"\x1C",",");
  449.         return s;
  450.     }
  451.  
  452.     function xrep(s, f, n) {
  453.         //IE,NS
  454.         //This function looks for any occurrence of the f string and replaces it with the n string.
  455.         //------------------------------
  456.         //Version 1.0
  457.         //
  458.         var tmp = s.split(f);
  459.         return tmp.join(n);
  460.     }
  461.  
  462.     function hNSCClick(e) {
  463.         //NS
  464.         //This function executes the selected command's trigger code.
  465.         //------------------------------
  466.         //Version 1.0
  467.         //
  468.         eval(this.TCode);
  469.     }
  470.     
  471.     function CBparseInt(n) {
  472.         //NS
  473.         //his function fixes a bug in Navigator's parseInt() function for the Mac.
  474.         //-----------------------------
  475.         //ersion 1.0
  476.         //
  477.         return IsMac?n:parseInt(n);
  478.     }
  479.  
  480.     function HideAll() {
  481.         //IE,NS
  482.         //This function will hide all the currently opened menus.
  483.         //------------------------------
  484.         //Version 1.0
  485.         //
  486.         while(nOM>0)
  487.             Hide();
  488.     }
  489.  
  490.     function GetLeftTop() {
  491.         //IE,NS
  492.         //This function returns the scroll bars position on the menus frame.
  493.         //------------------------------
  494.         //Version 1.0
  495.         //
  496.         if(IE)
  497.             return [mFrame.document.body.scrollLeft,mFrame.document.body.scrollTop];
  498.         if(NS)
  499.             return [mFrame.pageXOffset,mFrame.pageYOffset];
  500.     }
  501.  
  502.     function GetWidthHeight() {
  503.         //IE,NS
  504.         //This function returns the width and height of the menus frame.
  505.         //------------------------------
  506.         //Version 1.0
  507.         //
  508.         if(IE)
  509.             return [mFrame.document.body.clientWidth,mFrame.document.body.clientHeight];
  510.         if(NS)
  511.             return [mFrame.innerWidth,mFrame.innerHeight];
  512.     }
  513.  
  514.     function SetUpEvents() {
  515.         //IE,NS
  516.         //This function initializes the frame variables and setups the event handling.
  517.         //------------------------------
  518.         //Version 1.0
  519.         //
  520.         onerror = function(){;};
  521.         if(typeof(mFrame)=="undefined")
  522.         //MENUFRAME
  523.         if(typeof(mFrame)=="undefined")
  524.             window.setTimeout("SetUpEvents()",10);
  525.         else {
  526.             if(NS) {
  527.                 mFrame.captureEvents(Event.MOUSEMOVE);
  528.                 mFrame.onmousemove = HideMenus;
  529.                 mFrame.window.captureEvents(Event.MOUSEUP);
  530.                 mFrame.window.onmouseup = HideAll;    
  531.                 PrepareEvents();
  532.             }
  533.             mFrame.document.onmousemove = HideMenus;
  534.             mFrame.document.onclick = HideAll;
  535.         }
  536.     }
  537.  
  538.     function PrepareEvents() {
  539.         //NS
  540.         //This function is called right after the menus are rendered.
  541.         //It has been designed to attach the OnClick event to the <layer> tag. This is being
  542.         //done this way because Navigator does not support inline event capturing on the
  543.         //click event in the layer tag... duh!
  544.         //------------------------------
  545.         //Version 2.0
  546.         //
  547.         for(var l=0; l<mFrame.document.layers.length; l++) {
  548.             var lo = mFrame.document.layers[l];
  549.             lo.w = lo.clip.width;
  550.             lo.h = lo.clip.height;
  551.             for(var sl=0; sl<lo.layers.length; sl++) {
  552.                 var slo = mFrame.document.layers[l].layers[sl];
  553.                 if(slo.name.indexOf("EH")>0) {
  554.                     slo.document.captureEvents(Event.CLICK);
  555.                     slo.document.onclick = hNSCClick;
  556.                     slo.document.TCode = nTCode[slo.name.split("EH")[1]];
  557.                 }                    
  558.             }
  559.         }
  560.     }
  561.  
  562. //BROWSERCODE
  563. //EXPAND
  564.